<?php

namespace app\api\controller\web;


use addons\epay\library\Service;
use app\admin\model\Org;
use app\admin\model\Role;
use app\admin\model\Roomclean;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproombook;
use app\admin\model\Shoptuan;
use app\admin\model\Userbalance;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\common\controller\Api;
use Yansongda\Pay\Pay;

/**
 *
 */
class Order extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function getList(){

        $user = $this->auth->getUser();
        $orderNo = $this->request->param('orderNo');
        $startTime = $this->request->param('startTime');
        $endTime = $this->request->param('endTime');
        $status = $this->request->param('status');


        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();

        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $shop_id = $this->request->param('shop_id');
        $shopOrderModel = new Shoporder();
        $page = $this->request->param('page');
        $status = $this->request->param('status');
        $limit = 10;
        $offset = ($page - 1) * $limit;

        $where = array();
        $where2= array();
        if($shop_id != -1){
            $where['shop_id'] = $shop_id;
            $where2['shop_id'] = $shop_id;
        }


        if($startTime != ''){

            $where['create_time'] = ['between',[strtotime($startTime),strtotime($endTime)]];
            $where2['create_time'] = ['between',[strtotime($startTime),strtotime($endTime)]];
        }
        if($orderNo != ''){
            $where['order_no'] = ['like','%'.$orderNo.'%'];
            $where2['order_no'] = ['like','%'.$orderNo.'%'];
        }

        $where['Shoporder.org_id'] = $org['id'];
        $where2['org_id'] =  $org['id'];

        if($status != -1 && $status != ""){
            $where['Shoporder.status'] = array('=',$status);
            $where2['status'] =  array('=',$status);

        }
        $list =  $shopOrderModel->with(['user'])->where('Shoporder.status',"<>",'paying')->where($where)->order('create_time','desc')->limit($offset,$limit)->select();
        $total =  $shopOrderModel->where('status',"<>",'paying')->where($where2)->count('id');
        $totalAmount =  $shopOrderModel->where('status',"<>",'paying')->where($where2)->sum('amount');

        foreach ($list as $key=>$val){

            $val['user']->hidden(['password', 'salt']);
            $list[$key]['user_mobile'] = $val['user']['mobile'];
            $list[$key]['order_no_str'] = substr($val['order_no'],0,5)."*****".substr($val['order_no'],strlen($val['order_no'])-4,4);

            $list[$key]['book_start'] = date("Y-m-d H:i:s",$val['book_start']);
            $list[$key]['book_end'] = date("Y-m-d H:i:s",$val['book_end']);
            $list[$key]['status_txt'] = $shopOrderModel->formatStatus($val['status']);
        }
        $ret = array();
        $ret['list'] = $list;
        $ret['total'] = $total;
        $ret['totalAmount'] = $totalAmount;

        $this->success('',$ret);
    }


    public function detail(){

        $order_no = $this->request->param("order_no");
        $shopOrderModel = new Shoporder();
        $order = $shopOrderModel->with(['user'])->where('order_no',$order_no)->find();
        $order['tuan'] = "";
        if($order['tuan_id'] != -1){
            $shopTuanModel = new Shoptuan();
            $tuan = $shopTuanModel->where('id',$order['tuan_id'])->find();
            if($tuan){
                $order['tuan'] = $tuan['name'];
            }
        }
        $order['user']->hidden(['password', 'salt']);

        $order['status_txt'] = $shopOrderModel->formatStatus($order['status']);
        $order['book_start'] = date("Y-m-d H:i:s",$order['book_start']);
        $order['book_end'] = date("Y-m-d H:i:s",$order['book_end']);
        $this->success('',$order);
    }




}
